<template>
  <div class="work">

    <!-- 搜索框 -->
    <div class="search">
      <div class="title">
        Payroll Record
      </div>
      <div class="dateSearch">
          <div>
            <el-date-picker
                v-model="form.startTime"
                type="month"
                value-format='yyyy-MM'
                :picker-options="startTime"
                placeholder="select start time">
            </el-date-picker>
            <el-date-picker
                v-model="form.endTime"
                type="month"
                value-format='yyyy-MM'
                :picker-options="endTime"
                placeholder="select end time">
            </el-date-picker>
          </div>
          <div>
            <el-button type="info" plain @click="search" style="border-radius:0">Search</el-button>
            <el-button type="info" plain @click="clearSearch" style="margin-left:0;border-radius:0">Cancel search</el-button>
          </div>
      </div>
    </div>

    <!-- 主体内容 -->
    <template>
      <el-table
          :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
          :data="tableData"
          border
          style="width: 100%"
          ref="tableData"
          class="emp_table"
          v-if="tableData">
          <el-table-column label="S/N" type="index" width="60" align="center"></el-table-column>
          <el-table-column
          align="center"
          prop="date"
          label="Belong Time"
          width="180">
          </el-table-column>
          <el-table-column
          align="center"
          prop="payment_time"
          label="Pay Date">
          </el-table-column>
          <el-table-column
          align="center"
          prop="bill_number"
          label="Note number">
          </el-table-column>
        <el-table-column
          align="center"
          label="Action">
          <template slot-scope="scope">
              <el-button type="text" size="small" @click="see(scope.row.id)">See Details</el-button>
          </template>
          </el-table-column>
      </el-table>
    </template>

    <!--分页-->
    <el-pagination
      align="right"
      @current-change="curChange"
      :current-page.sync="curPage"
      :page-size="10"
      layout="total,prev, pager, next, jumper"
      :total="count">
    </el-pagination>

    <seeForm ref="seeForm"></seeForm>

  </div>


</template>

<script>
import { getPayrollRecordList } from "@/request/other_api";
import seeForm from "./seeForm";

export default {
    components:{
    seeForm,
  },
  data() {
    return {
        // 搜索内容
        searchName:'',
        // 页面数据
        tableData: [],
        // count:总数据量
        count:0,
        // 当前页
        curPage:1,
        // 弹出框是否可见
        dialogVisible: false,
        // 等级
        level:'',
        //搜索时间
        form:{
            startTime:undefined,
            endTime:undefined,
        },
        startTime: {
            disabledDate: time => {
            let endDateVal = this.form.endTime;
            if (endDateVal) {  return time.getTime() > new Date(endDateVal).getTime()}
            }
        },
        endTime: {
            disabledDate: time => {
            let beginDateVal = this.form.startTime;
            if (beginDateVal) { return time.getTime() < new Date(beginDateVal).getTime()}
            }
        },
        // 是否在搜索状态
        isSearch:false,
        key:Number,
    }
  },
  mounted () {
    this.level = sessionStorage.getItem('level')
  },
  created() {
    //获取初始列表数据
    getPayrollRecordList().then(res => {
      if(res.code == 200){
        this.count = res.count
        this.tableData = res.data
      }
    })
  },
  methods: {
    // 搜索
    search(){
      this.isSearch = true
      getPayrollRecordList({
        startDate:this.form.startTime,
        endDate:this.form.endTime,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
      this.curPage = 1
    },
    // 取消搜索
    clearSearch(){
      this.isSearch = false
      getPayrollRecordList().then(res => {
        if(res.code === 200){
            this.count = res.count
            this.tableData = res.data
        }
      })
      this.curPage = 1
      this.form.startTime = ''
      this.form.endTime = ''
    },
    //查看报销单详情
    see(id){
      this.$refs.seeForm.formatData(id)
      this.$refs.seeForm.dialogVisible = true
    },
    /**
     * 分页
     */
    //  点击每页刷新获取数据
    curChange (page) {
      this.curPage = page
      if(this.isSearch) {
        this.getPayrollRecordSearchList()
      } else {
        this.getPayrollRecordList()
      }
    },
    // 上一页
    prevClick () {
    this.curPage < 1 ? this.curPage = 1 : this.curPage--
      if(this.isSearch) {
        this.getPayrollRecordSearchList()
      } else {
        this.getPayrollRecordList()
      }
    },
    // 下一页
    nextClick () {
    this.curPage >= this.count ? this.curPage = this.count : this.curPage++
      if(this.isSearch) {
        this.getPayrollRecordSearchList()
      } else {
        this.getPayrollRecordList()
      }
    },
    //获取搜索列表分页
    getPayrollRecordSearchList(){
      getPayrollRecordList({
        page: this.curPage,
        startDate:this.form.startTime,
        endDate:this.form.endTime,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
    },
    //获取已有报销单列表分页
    getPayrollRecordList(){
      getPayrollRecordList({
        page: this.curPage,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
    },
  },
}
</script>

<style lang="less">
    .permitImg{
      width: 35px;
      height: 35px;
    }
    .dig-form{
      width: 90%;
      height: 100%;
      margin: 0 auto;
    }
    .preImg{
      width:56px;
      border-radius:4px;
      height:40px;
      margin-right:15px
    }
    .title{
      font-family: SourceHanSansSC;
      font-weight: 700;
      font-size: 20px;
      color: rgb(16, 16, 16);
      font-style: normal;
      letter-spacing: 0px;
      line-height: 30px;
      text-decoration: none;
      margin-bottom: 10px;
    }
    .search {
      display: flex;
      justify-content: space-between;
      padding-bottom: 20px;
    }
    .dateSearch {
      display: flex;
      justify-content: space-between;
      padding-bottom: 0px;
      border-radius:0
    }
    /deep/ .el-button {
      border-radius: 0;
    }
    /deep/ .el-date-picker {
      border-radius: 0
    }
</style>


